package com.company.crm.settings.web.interceptor;

import com.company.crm.commons.constants.Constants;
import com.company.crm.settings.domain.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：王磊栋
 * 2021/4/13
 */
//登陆拦截器，没有登录就得被拦截返回登录页面
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        User user = (User) httpServletRequest.getSession().getAttribute(Constants.SESSION_USER);
        if (user==null){
            //证明没有成功登陆过，进行拦截并进行跳转。
            httpServletResponse.sendRedirect(httpServletRequest.getContextPath());
            return  false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
